home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / packages / uncompress.el.z / uncompress.el
Encoding:
Text File  |  1998-05-21  |  3.6 KB  |  101 lines

  1. ;;; uncompress.el --- auto-decompression hook for visiting .Z files
  2.  
  3. ;; Copyright (C) 1992, 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6. ;; Keywords: unix extensions
  7.  
  8. ;; This file is part of XEmacs.
  9.  
  10. ;; XEmacs is free software; you can redistribute it and/or modify it
  11. ;; under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; XEmacs is distributed in the hope that it will be useful, but
  16. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18. ;; General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  22. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  23. ;; 02111-1307, USA.
  24.  
  25. ;;; Synched up with: 19.34.
  26.  
  27. ;;; Commentary:
  28.  
  29. ;; This package can be used to arrange for automatic uncompress of
  30. ;; files packed with the UNIX compress(1) utility when they are visited.
  31. ;; All that's necessary is to load it.  This can conveniently be done from
  32. ;; your .emacs file.
  33.  
  34. ;;; Code:
  35.  
  36. ;; When we are about to make a backup file,
  37. ;; uncompress the file we visited
  38. ;; so that making the backup can work properly.
  39. ;; This is used as a write-file-hook.
  40.  
  41. (defvar uncompress-program "gunzip"
  42.   "Program to use for uncompression.")
  43.  
  44. (defun uncompress-backup-file ()
  45.   (and buffer-file-name make-backup-files (not buffer-backed-up)
  46.        (not (file-exists-p buffer-file-name))
  47.        (call-process uncompress-program nil nil nil buffer-file-name))
  48.   nil)
  49.  
  50. (or (assoc "\\.Z$" auto-mode-alist)
  51.     (setq auto-mode-alist
  52.       (cons '("\\.Z$" . uncompress-while-visiting) auto-mode-alist)))
  53. (or (assoc "\\.gz$" auto-mode-alist)
  54.     (setq auto-mode-alist
  55.       (cons '("\\.gz$" . uncompress-while-visiting) auto-mode-alist)))
  56.  
  57. (defun uncompress-while-visiting ()
  58.   "Temporary \"major mode\" used for .Z and .gz files, to uncompress them.
  59. It then selects a major mode from the uncompressed file name and contents."
  60.   (if (and (not (null buffer-file-name))
  61.        (string-match "\\.Z$" buffer-file-name))
  62.       (set-visited-file-name
  63.        (substring buffer-file-name 0 (match-beginning 0)))
  64.     (if (and (not (null buffer-file-name))
  65.          (string-match "\\.gz$" buffer-file-name))
  66.     (set-visited-file-name
  67.      (substring buffer-file-name 0 (match-beginning 0)))))
  68.   (message "Uncompressing...")
  69.   (let ((buffer-read-only nil))
  70.     (shell-command-on-region (point-min) (point-max) uncompress-program t))
  71.   (message "Uncompressing...done")
  72.   (set-buffer-modified-p nil)
  73.   (make-local-variable 'write-file-hooks)
  74.   (or (memq 'uncompress-backup-file write-file-hooks)
  75.       (setq write-file-hooks (cons 'uncompress-backup-file write-file-hooks)))
  76.   (normal-mode))
  77.  
  78. (or (memq 'find-compressed-version find-file-not-found-hooks)
  79.     (setq find-file-not-found-hooks
  80.       (cons 'find-compressed-version find-file-not-found-hooks)))
  81.  
  82. (defun find-compressed-version ()
  83.   "Hook to read and uncompress the compressed version of a file."
  84.   ;; Just pretend we had visited the compressed file,
  85.   ;; and uncompress-while-visiting will do the rest.
  86.   (let (name)
  87.     (if (file-exists-p (setq name (concat buffer-file-name ".Z")))
  88.     (setq buffer-file-name name)
  89.       (if (file-exists-p (setq name (concat buffer-file-name ".gz")))
  90.       (setq buffer-file-name name)))
  91.     (if (eq name buffer-file-name)
  92.     (progn
  93.       (insert-file-contents buffer-file-name t)
  94.       (goto-char (point-min))
  95.       (setq error nil)
  96.       t))))
  97.  
  98. (provide 'uncompress)
  99.  
  100. ;;; uncompress.el ends here
  101.